/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controls;

import com.jme3.collision.CollisionResults;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;

/**
 *
 * @author dani
 */
public class TargetControl extends AbstractControl {

    private Node enemyNode;

    public TargetControl(Node enemyNode) {
        this.enemyNode = enemyNode;
    }

    @Override
    protected void controlUpdate(float tpf) {
        if (enemyNode.getChildren().size() > 0) {
            CollisionResults results = new CollisionResults();
            Spatial target = getSpatial();
            enemyNode.collideWith(target, results);
            for (int i = 0; i < results.size(); i++) {
                Spatial enemySpatial = results.getCollision(i).getGeometry();
                enemySpatial.setUserData("hp", 0);

                Integer targetHp = spatial.getUserData("hp");
                target.setUserData("hp", targetHp - 5);

                // For each hit, we know distance, impact point, name of geometry.
                float dist = results.getCollision(i).getDistance();
                Vector3f pt = results.getCollision(i).getContactPoint();
                String hit = results.getCollision(i).getGeometry().getName();
                System.out.println("* Collision #" + i);
                System.out.println("  You shot " + hit + " at " + pt + ", " + dist + " wu away." + results.getCollision(i).getGeometry().getParent().getName());
            }
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }
}
